import React, { Component } from 'react'
import { nanoid } from 'nanoid'
import './index.css'

export default class Header extends Component {
  handleKeyUp = (e) => {
    const { keyCode, target } = e
    if (keyCode !== 13) return
    const { value } = target
    if (value.trim() === '') return
    const todo = {
      id: nanoid(),
      text: value,
      done: false
    }
    this.props.addTodo(todo)
    e.target.value = ''
  }
  render () {
    return (
      <div className='todo-header'>
        <input type="text" placeholder='请输入你的任务名称，按回车键确认' onKeyUp={this.handleKeyUp} />
      </div>
    )
  }
}
